package com.gylang.crawler.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gylang.crawler.domain.MovieSimpleInfo;
import com.gylang.crawler.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author gylang
 * data 2021/5/25
 */
@Component
@Slf4j
public class GetMovieIntroHandler {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public int doStore(Integer start) throws Exception {
        String url = "https://movie.douban.com/j/new_search_subjects?sort=U&tags=&start=" + start + "&limit=100";
        String body = HttpUtil.createGet(url)
                .execute()
                .body();
        R<List<MovieSimpleInfo>> list = null;
        try {
            list = JSON.parseObject(body, new TypeReference<R<List<MovieSimpleInfo>>>() {
            });
            if (CollUtil.isEmpty(list.getData())) {
                list = doProxy(url);
            }
        } catch (Exception e) {
            e.printStackTrace();
             list = doProxy(url);
        }
        if (null == list || null == list.getData()) {
            return 0;
        }
        //存入es
        BulkRequest request = new BulkRequest();
        for (MovieSimpleInfo simpleInfo : list.getData()) {
            IndexRequest indexRequest = new IndexRequest("movie", "doc", simpleInfo.getId());
            indexRequest.source(BeanUtil.beanToMap(simpleInfo));
            request.add(indexRequest);
        }

        elasticsearchRestTemplate.getClient()
                .bulk(request, RequestOptions.DEFAULT);

        return list.getData().size();

    }

    private R<List<MovieSimpleInfo>> doProxy(String url) {
        System.out.println("尝试走代理");
        HttpRequest httpRequest = HttpUtil.createPost("https://getman.cn/api/request")
                .form("url", url)
                .form("method", "GET");
        String body = httpRequest
                .execute().body();
        JSONObject jsonObject = JSON.parseObject(body);
        String string = jsonObject.getJSONObject("data").getString("body");
        return JSON.parseObject(string, new TypeReference<R<List<MovieSimpleInfo>>>() {
        });
    }
}
